package com.beta.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;
import com.beta.dto.admin.AdminDto;
import com.beta.entity.Admins;
import com.beta.entity.AdminsWithRoles;
import com.beta.entity.Cities;
import com.beta.mapper.AdminsMapper;
import com.beta.service.IAdminsService;
import com.beta.service.IAdminsWithRolesService;
import com.beta.service.ICitiesService;
import com.beta.service.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * Admins 表数据服务层接口实现类
 */
@Service
public class AdminsService extends SuperServiceImpl<AdminsMapper, Admins> implements IAdminsService {
    @Autowired
    private ICitiesService citiesService;
    @Autowired
    private AdminsMapper adminsMapper;
    @Autowired
    private DateTimeService dbService;
    @Autowired
    private IAdminsWithRolesService adminsWithRolesService;

    @Override
    public Admins findAdminByName(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }
        return baseMapper.selectOne(new Admins().setUsername(username));
    }

    @Override
    public Page<AdminDto> getAdmins(String userName, Page<AdminDto> page) {
        page.setRecords(adminsMapper.selectAdmins(userName, page));
        return page;
    }


    @Override
    public AdminDto getAdminById(Integer adminId) {
        if (Objects.isNull(adminId)) {
            throw new ServiceException("admins.invalid.id");
        }
        return adminsMapper.getAdminById(adminId);
    }

    @Override
    public void executeCreateAdmin(AdminDto adminDto) {
        Cities cities = citiesService.selectOne(new Cities().setCode(adminDto.getCityCode()));
        if (Objects.isNull(cities)) {
            throw new ServiceException("cities.invalid.city");
        }
        Admins entity = new Admins().setUsername(adminDto.getUsername())
                .setEncryptedPassword(adminDto.getEncryptedPassword())
                .setEmail(adminDto.getEmail())
                .setCreatedAt(dbService.toDbDate())
                .setUpdatedAt(dbService.toDbDate())
                .setCityCode(adminDto.getCityCode())
                .setCityName(cities.getName());
        insertSelective(entity);
        Integer adminId = selectOne(new Admins().setUsername(adminDto.getUsername())).getId();
        adminsWithRolesService.insertSelective(new AdminsWithRoles()
                .setAdminId(adminId)
                .setAdminRoleId(adminDto.getRoleId()));
    }

    @Override
    public void executeUpdateAdmin(Integer adminId, AdminDto adminDto) {
        if (Objects.isNull(adminId)) {
            throw new ServiceException("admins.invalid.id");
        }
        Admins updateAdminsEntity = new Admins().setUsername(adminDto.getUsername())
                .setEncryptedPassword(adminDto.getEncryptedPassword())
                .setEmail(adminDto.getEmail())
                .setUpdatedAt(dbService.toDbDate());
        if (Objects.isNull(updateAdminsEntity)) {
            throw new ServiceException("admins.invalid.admin");
        }
        updateSelective(updateAdminsEntity, new Admins().setId(adminId));
        AdminsWithRoles roleUpdateEntity = new AdminsWithRoles().setAdminRoleId(adminDto.getRoleId());
        adminsWithRolesService.updateSelective(roleUpdateEntity, new AdminsWithRoles().setAdminId(adminId));
    }
}